<template>
  <DataList ref="datalist"
  :cate="cate" 
  :cols="cols"
  :name="name"
  :excelHeader="excelHeader"
  :groupCheck = "groupCheck"
  skipExcelNumber="4" >
  <template slot="search">
     <el-form-item >
        <el-input v-model="search_str"  @change="clear" @input="mysearch" placeholder="请输入课程名搜索" clearable></el-input>
      </el-form-item>
  </template>
  </DataList>
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import linq from "linq"
import {findComponent} from "@/utils/ui";
import {get开设专业} from  "@/api/开设专业"
import { get课程设置,update课程设置,delete课程设置,add课程设置,bulkAdd课程设置 } from '@/api/课程设置.js'
export default {
  components: {
    DataList
  },
  data() {
    return {
      cate: "A7-2课程设置表",
      name: "7.2 课程设置",
      search_str:'',
      excelHeader:{B3:"专业所属系部代码",C3:"专业所属系部名称",D3:"专业代码",E3:"专业名称（全称）",F3:"专业方向代码",G3:"专业方向名称（全称）",H3:"课程代码",I3:"课程名称",J3:"课程类型",K3:"课程属性",L3:"课程性质",M3:"教学计划规定课时数（学时）",N3:"实践课程",N5:"课时数",O5:"比例（%）",P3:"是否专业核心课程",Q3:"是否校企合作开发课程 ",R3:"精品课程",S3:"使用教材",S4:"教材名称(全称)",T4:"版本日期（年）",U4:"出版社",V4:"第一作者",W4:"教材性质",W5:"教育部规划教材",X5:"教育部精品教材",Y5:"行业部委统编教材",Z5:"校企合作开发教材",AA5:"自编教材",AB5:"讲义",AC4:"教材类型",AD3:"授课年级",AE3:"主要授课地点",AF3:"主要授课方式",AG3:"考试/考核主要方法",AH3:"课证融通课程"},

      cols: [
        { label: "专业所属系部代码", prop: "BMBH" },
        { label: "专业所属系部名称", prop: "BMMC" },
        { label: "专业代码", prop: "ZYDM" },
        { label: "专业名称(全称)", prop: "ZYMC" },
        { label: "专业方向代码", prop: "ZYFXDM" },
        { label: "专业方向名称", prop: "ZYFXMC" },
        { label: "课程代码", prop: "KCDM" },
        { label: "课程名称", prop: "KCMC" },
        { label: "课程类型", prop: "XZ_KCLX" },
        { label: "课程属性", prop: "XZ_KCSX" },
        { label: "课程性质", prop: "XZ_KCXZ" },
        { label: "教学计划规定课时数(学时)", prop: "ZXS" },
        { label: "实践课程", data:[
            { label: "实践课程_课时数", prop: "SJKCKSS" },
            { label: "实践课程_比例(%)", prop: "SJKSBL" }
            ]},
        { label: "是否专业核心课程", prop: "XZ_SFHXK" },
        { label: "是否校企合作开发课程", prop: "XZ_SFXQHZ" },
        { label: "精品课程", prop: "XZ_JPKC" },
        
        { label: "使用教材" ,data:[
            {label:"教材名称",prop:"JCMC"},{ label: "版本日期(年)", prop: "CBRQ" },
        { label: "出版社", prop: "CBS" },{ label: "第一作者", prop: "BZZXM" },
        {label:"教材性质",data:[
            {label:"是否教育部规划教材",prop:"XZ_JCXZ1"},{ label: "是否教育部精品教材", prop: "XZ_JCXZ2" },
        { label: "是否行业部委统编教材", prop: "XZ_JCXZ3" },{ label: "是否校企合作开发教材", prop: "XZ_JCXZ4" },
        { label: "是否自编教材", prop: "XZ_JCXZ5" },{ label: "是否讲义", prop: "XZ_JCXZ6" }
        ]},
        {label:"教材类型",prop:"XZ_JCLX"}
        ]},
            {label:"授课年级",prop:"XZ_SKNJ"},
            {label:"主要授课地点",prop:"XZ_JXDD"},
            {label:"主要授课方式",prop:"SKFSM"},
            {label:"考试/考核主要方法",prop:"KSFSM"},
            {label:"课证融通课程",prop:"XZ_SFKZRT"}

      ],
      groupCheck:[{dada:["SJKCKSS","SJKSBL"],desc:'请完善实践课程的输入'},
      {dada:["JCMC","CBRQ","CBS","BZZXM"],desc:'请完善使用教材的输入'},
      {dada:["XZ_JCXZ1","XZ_JCXZ2","XZ_JCXZ3","XZ_JCXZ4","XZ_JCXZ5","XZ_JCXZ6"],desc:'请完善教材性质的输入'}]
    };
  },
  methods: {
    getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
         sums[0]="合计";
         sums[6]="系部数:"+linq.from(list).distinct("$.BMBH").count()
         sums[8]="专业数:"+linq.from(list).distinct("$.ZYDM").count()
         sums[10]="方向数:"+linq.from(list).distinct("$.ZYFXDM").count()
         sums[12]="课程数:"+linq.from(list).distinct("$.KCDM").count()
         sums[14]= "A类:"+linq.from(list).where("$.XZ_KCLX=='A类'").count();
         sums[14]+= ",B类:"+linq.from(list).where("$.XZ_KCLX=='B类'").count();
         sums[14] += ",C类:"+linq.from(list).where("$.XZ_KCLX=='C类'").count();
         sums[17]="平均:"+linq.from(list).average("$.ZXS").toFixed(0)
         sums[18]="平均:"+linq.from(list).average("$.SJKCKSS").toFixed(0)
         sums[19]="平均:"+linq.from(list).average("$.SJKSBL").toFixed(2)
         sums[20]= "核心课:"+linq.from(list).sum("$.XZ_SFHXK == '是'")
         sums[21]= "校企合作:"+linq.from(list).sum("$.XZ_SFXQHZ == '是'")
         sums[22]="精品课:"+linq.from(list).where("$.XZ_JPKC").count()
         sums[23]="教材数:"+linq.from(list).distinct("$.JCMC").count()
         sums[38]= "课证融通课:"+linq.from(list).sum("$.XZ_SFKZRT == '是'")
         sums[39]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
         sums[39]+= ",不通过:"+linq.from(list).where("$.审核状态=='不通过'").count();
         sums[39] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
 
       return sums;
      //
    },
      onDlgCreated(model) {
       
      var dd = findComponent(this,"ZYFXMC");
     
      var 专业 = linq.from(this.$root.开设专业).distinct("$.ZYFXDM").toArray();
      
      dd.$attrs.items = 专业.map(item => {
        return { label: item.ZYFXMC, value: item.ZYFXDM};
      });
    },
     async onChange(model,field,value){
       //
       //
       model.SJKSBL = ((Number(model.SJKCKSS)/Number(model.ZXS))*100).toFixed(2)
       //
       if(field == "ZYFXMC"){
         //
         var 专业 = linq.from(this.$root.开设专业).firstOrDefault(`$.ZYFXDM=='${value}'`);
          model.BMBH = 专业.BMBH;
          model.BMMC = 专业.BMMC;
            model.ZYDM = 专业.ZYDM;
              model.ZYMC = 专业.ZYMC;
             
                model.ZYFXDM = 专业.ZYFXDM
                  model.ZYFXMC = 专业.ZYFXMC
         //
       }
     },
    calField(list){
      list.forEach(model=>{
        model.SJKSBL = ((Number(model.SJKCKSS)/Number(model.ZXS))*100).toFixed(2)
        model.SJKSBL=isNaN(model.SJKSBL)?0:model.SJKSBL;
        
      })
     },
    /*onadd(model){
      if(model.使用教材_教材名称!=""||model.使用教材_版本日期!=""||model.使用教材_出版社!=""||model.使用教材_第一作者!=""||model.使用教材_教材性质_是否教育部规划教材!=""||model.使用教材_教材性质_是否教育部精品教材!=""
      ||model.使用教材_教材性质_是否行业部委统编教材!=""||model.使用教材_教材性质_是否校企合作开发教材!=""||model.使用教材_教材性质_是否自编教材!=""||model.使用教材_教材性质_是否讲义!=""||model.使用教材_教材类型!=""){
         if(model.使用教材_教材名称==""||model.使用教材_版本日期==""||model.使用教材_出版社==""||model.使用教材_第一作者==""||model.使用教材_教材性质_是否教育部规划教材==""||model.使用教材_教材性质_是否教育部精品教材==""
      ||model.使用教材_教材性质_是否行业部委统编教材==""||model.使用教材_教材性质_是否校企合作开发教材==""||model.使用教材_教材性质_是否自编教材==""||model.使用教材_教材性质_是否讲义==""||model.使用教材_教材类型==""){
            return "使用教材中【教材名称】、【版本时期】、【出版社】、【第一作者】、【教材性质】和【教材类型】等字段，如果填了其中1个，其他的必须填写！";
         }
      }
    },*/
     check(array,list) {
     return new Promise(async (resolve,reject)=>{
         try{
          var user = getUser();
          //var 专业 = await get开设专业(user.XXID,user.CJPC)
          
          // var duplicates =  linq.from(array).groupBy("$.KCDM").where("$.count()>1").toArray();
          // duplicates.forEach(e=>{
          //   linq.from(array).where(t=>t.KCDM==e.key()).forEach(t=>t.error="录入数据存在相同的课程");
          // })
           var err = linq.from(array).select("$.ZYDM+','+$.ZYFXDM").except(linq.from(this.$root.开设专业).select("$.ZYDM+','+$.ZYFXDM").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.ZYDM+','+e.ZYFXDM) !==-1).forEach(e=>{
             
                  e.error=e.error?e.error+";":''+"开设专业表不存在对应的专业代码与专业方向代码组合合";
               
          })
           
           
          //是否已经存在数据
         
          // var err =  linq.from(array).select("$.KCDM").intersect(linq.from(list).select("$.KCDM").toArray()).toArray();
          // linq.from(array).where(e=>err.indexOf(e.KCDM) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"数据库中已经存在课程");
          
          //把错误的班级代码和班级名称，学生姓名替代掉
          err =  linq.from(array).select("$.ZYDM+','+$.ZYMC+','+$.ZYFXDM+','+$.ZYFXMC").except(linq.from(this.$root.开设专业).select("$.ZYDM+','+$.ZYMC+','+$.ZYFXDM+','+$.ZYFXMC").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.ZYDM+','+e.ZYMC+','+e.ZYFXDM+','+e.ZYFXMC) !==-1).
          forEach(e=>{
            var d = linq.from(this.$root.开设专业).firstOrDefault(item=>item.ZYDM == e.ZYDM && item.ZYFXDM == e.ZYFXDM);
            if(d){
              e.ZYMC = d.ZYMC;
              e.ZYFXMC = d.ZYFXMC;
              e.BMMC = d.BMMC;
              e.BMBH = d.BMBH;
            }
          });
          resolve();
          }catch(e){
            reject(e);
          }
         
      })},
    async  bulkAdd(array){
       await  bulkAdd课程设置({Kcsz:array});
    },
    async fetchData() {
      console.log(1)
      var user = getUser();
      var datas = await get课程设置(user.XXID,user.CJPC);
      return datas;
    },
    onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
      
          console.log(JSON.stringify(temp));
          var model = await add课程设置(temp);
          resolve(model);
      })
      
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
          console.log(JSON.stringify(temp));
          var model = await update课程设置(temp);
          resolve(model);
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
         await delete课程设置(temp);
          resolve();
      })
    }
  }
};
</script>
<style>
</style>